/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
 */
/*
 * Chain.java
 *
 * Created on January 20, 2003, 3:32 PM
 */

package com.webdeninteractive.bie.persistent.privileges;

import java.sql.*;
import java.util.*;
import com.webdeninteractive.bie.persistent.*;
import java.io.*;
/**
 *
 * @author  jdepons
 */
public class Resource {
    
    private String name;
    private int oid;
    private String description;
    
    /** Creates a new instance of Chain */
    private Resource(int oid, String name) {
        this.oid = oid;
        this.name = name;
    }
    
    public int getOID() {
        return this.oid;       
    }

    public String getName() {
        return this.name;
    }
    
    public String getDescription() {
        return this.description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public static ArrayList findAll() throws SQLException,IOException{    
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            ArrayList al = null;
            al = new ArrayList();
            rs = s.executeQuery("select * from Resources");
            
            while (rs.next()) {
                al.add(load(rs));
            }
            return al;
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }    
    }
        
    public static Resource findByOID(int OID) throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            rs = s.executeQuery("select * from Resources where OID = " + OID);
            rs.next();
            return load(rs);
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
    
    
    }
    
    public static Resource findByName(String name) throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            
            rs = s.executeQuery("select * from Resources where name = '" + name + "'");
            rs.next();
            return load(rs);
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }        
    
    }
    
    public static Resource create (String name) throws SQLException,IOException{        
        int resourceIncrementer =  Controller.getInstance().generateOID("Resources");
        
        Connection conn = null;
        Statement s = null;
        
        try {
            
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            
            String sql = "insert into Resources (OID,name,description) values (" + resourceIncrementer + ",'" + name + "','')";                                    
            s.execute(sql);

            conn.commit();
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
        
        Resource resource = new Resource(resourceIncrementer,name);
        return resource;                        
    }
    
    public void remove() throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
                
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            String sql ="delete from Resources where OID=" + this.oid;;
            s.execute(sql);
            
            sql ="delete from Role_Resource where resourceOID=" + this.oid;;
            s.execute(sql);
                        
            conn.commit();            
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }        
    }
            
    public void commitObject() throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            String sql ="update Resources set description='" + this.description + "' where OID=" + this.oid;
            s.execute(sql);
            conn.commit();
            
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
        
    }

    private static Resource load(ResultSet rs) throws SQLException,IOException{
        Resource resource = new Resource(rs.getInt("OID"),rs.getString("name"));
        resource.setDescription(rs.getString("description"));
        return resource;
    }
    
}
